import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

url = "https://api.github.com/search/repositories?q=language:python&sort=stars"

response = requests.get(url)
print("status code ", str(response.status_code))

res_dict = response.json()
repo_dicts = res_dict["items"]

names, stars = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict["name"])
    description="默认描述"
    if  repo_dict["description"]:
        description=repo_dict["description"]
    dict = {
        "value": repo_dict["stargazers_count"],
        "label": description,
        "xlink":repo_dict["html_url"]
    }
    stars.append(dict)

git_star_style = LS("#336699", base_style=LCS)

my_config = pygal.Config()
# 横坐标标签旋转45度
my_config.x_label_rotation = 45
# 隐藏图例,就是图表左侧表明各条形图是干啥的那个
my_config.show_legend = False

my_config.title_font_size = 24
my_config.label_font_size = 14
# 主标签的字体
my_config.major_label_font_size = 18
# 将较长的项目名缩短为15个字符
my_config.truncate_label = 15
# y坐标中的虚线,这个虚线是表示1个区间的分界线
my_config.show_y_guides = False
# 设置了自定义宽度，让图表更充分地利用浏览器中的可用空间
my_config.width = 1000
my_config.style = git_star_style
my_config.title = "git star 条形图"

chart = pygal.Bar(my_config)
chart.x_labels = names

chart.add("", stars)
chart.render_to_file("git star 条形图.svg")
